﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FisApp.utility;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using System.Data.SqlClient;
using FisApp.usercontrol;
using FisApp.enums;

namespace FisApp
{
    public partial class FrmModelImages : Form
    {
        #region Variables...
        public int? modelId;

        public int? kalite
        {
            get
            {
                return (int?)kaliteLookUp.EditValue;
            }
            set
            {
                kaliteLookUp.EditValue = value;
            }
        }

        public int? taban
        {
            get
            {
                return (int?)tabanLookUp.EditValue;
            }
            set
            {
                tabanLookUp.EditValue = value;
            }
        }

        public int? renk
        {
            get
            {
                return (int?)renkLookUp.EditValue;
            }
            set
            {
                renkLookUp.EditValue = value;
            }
        }

        public int? deri
        {
            get
            {
                return (int?)deriLookUp.EditValue;
            }
            set
            {
                deriLookUp.EditValue = value;
            }
        }
        #endregion

        #region Constructor...
        public FrmModelImages()
        {
            InitializeComponent();
        }
        #endregion

        #region Methods....
        private void clearForm()
        {
            modelId = null;
            kalite = null;
            taban = null;
            renk = null;
            deri = null;
        }

        private void LoadKalite()
        {
            try
            {
                string query = "SELECT * FROM kalite";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                {
                    kaliteLookUp.Properties.DataSource = ds.Tables[0];
                    kaliteLookUp.Properties.DisplayMember = "kalite_no";
                    kaliteLookUp.Properties.ValueMember = "kalite_id";
                }
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void LoadTaban()
        {
            try
            {
                string query = "SELECT * FROM taban WHERE kalite_id=@kalite_id";

                List<SqlParameter> list = new List<SqlParameter>();
                list.Add(DBUtils.GetParameter("@kalite_id", kalite));
                DataSet ds = DBUtils.SQLSelect(query, list);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                {
                    tabanLookUp.Properties.DataSource = ds.Tables[0];
                    tabanLookUp.Properties.DisplayMember = "taban_no";
                    tabanLookUp.Properties.ValueMember = "taban_id";
                }
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void LoadRenk()
        {
            try
            {
                string query = "SELECT * FROM renk";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                {
                    renkLookUp.Properties.DataSource = ds.Tables[0];
                    renkLookUp.Properties.DisplayMember = "renk_kodu";
                    renkLookUp.Properties.ValueMember = "renk_id";
                }
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void LoadDeri()
        {
            try
            {
                string query = "SELECT * FROM deri";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                {
                    deriLookUp.Properties.DataSource = ds.Tables[0];
                    deriLookUp.Properties.DisplayMember = "deri_kodu";
                    deriLookUp.Properties.ValueMember = "deri_id";
                }
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }
        #endregion

        #region Events...
        private void FrmModelImages_Load(object sender, EventArgs e)
        {
            LoadKalite();
            LoadTaban();
            LoadRenk();
            LoadDeri();
            LoadModeller();
        }

        private void LoadModeller()
        {
            try
            {
                string query = @"SELECT m.model_id, k.kalite_no + ' - ' + k.kalip_no as kalitebilgileri
                                    ,t.taban_marka + ' - ' + t.taban_no + ' - ' + t.taban_renk as tabanbilgileri
                                    ,d.deri_kodu + ' - ' + d.deri_aciklama as deribilgileri
                                    ,r.renk_kodu + ' - ' + r.renk_aciklama as renkbilgileri
                                    ,a.astar_kodu + ' - ' + a.astar_aciklama as astarbilgileri
                                    ,l.lazer_kodu + ' - ' + l.lazer_aciklama as lazerbilgileri
                                    ,ms.makinesarac_kodu + ' - ' + ms.makinesarac_aciklama as makinesaracbilgileri
                                    ,e.elsarac_kodu + ' - ' + e.elsarac_aciklama as elsaracbilgileri
                                    
                                    FROM model m 
                                   JOIN kalite k ON k.kalite_id = m.kalite_id
                                   JOIN taban t ON t.taban_id = m.taban_id
                                   JOIN renk r ON r.renk_id = m.renk_id
                                   JOIN deri d ON d.deri_id = m.deri_id
                              LEFT JOIN astar a ON a.astar_id = m.astar_id
                              LEFT JOIN lazer l ON l.lazer_id = m.lazer_id
                              LEFT JOIN elsarac e ON e.elsarac_id = m.elsarac_id
                              LEFT JOIN makinesarac ms ON ms.makinesarac_id = m.makinesarac_id
                                  WHERE 1=1";

                List<SqlParameter> list = new List<SqlParameter>();
                if (kalite != null)
                {
                    query += " AND m.kalite_id=@kalite_id";
                    list.Add(DBUtils.GetParameter("@kalite_id", kalite));
                }
                if (taban != null)
                {
                    query += " AND m.taban_id=@taban_id";
                    list.Add(DBUtils.GetParameter("@taban_id", taban));
                }
                if (deri != null)
                {
                    query += " AND m.deri_id=@deri_id";
                    list.Add(DBUtils.GetParameter("@deri_id", deri));
                }
                if (renk != null)
                {
                    query += " AND m.renk_id=@renk_id";
                    list.Add(DBUtils.GetParameter("@renk_id", renk));
                }

                DataSet ds = DBUtils.SQLSelect(query, list);

                xtraScrollableControl2.Controls.Clear();
                int width = xtraScrollableControl2.Size.Width;
                int height = xtraScrollableControl2.Size.Height;

                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    Label lbl = new Label();
                    lbl.Text = "Aradığınız kriterlere göre model bulunmamaktadır.";
                    lbl.AutoSize = true;
                    lbl.Location = new Point(width/2 - 200, height/2 - lbl.Height/2);
                    lbl.Font = new System.Drawing.Font("Verdana", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
                    lbl.ForeColor = System.Drawing.Color.Maroon;
                    xtraScrollableControl2.Controls.Add(lbl);
                    return;
                }



                int columnSize = width / 440;
                int firstColumn = (width % 440) / (columnSize + 1);

                UcModel[] ucModel = new UcModel[ds.Tables[0].Rows.Count];
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    int column = i % columnSize;
                    int row = i / columnSize;

                    ucModel[i] = new UcModel();
                    try
                    {
                        ucModel[i].peResim.EditValue = new Bitmap(ds.Tables[0].Rows[i]["image_path"].ToString());
                    }
                    catch (Exception ex)
                    {
                        ucModel[i].peResim.EditValue = FisApp.Properties.Resources.not_found;
                    }
                    //ucModel[i].peResim.Name = ds.Tables[0].Rows[i]["model_id"].ToString();
                    ////ucModel[i].lblKalite.Text = ds.Tables[0].Rows[i]["kalite_no"].ToString();
                    //ucModel[i].lblKalite.Text = ds.Tables[0].Rows[i]["deneme"].ToString();
                    //ucModel[i].lblTaban.Text = ds.Tables[0].Rows[i]["taban_no"].ToString();
                    //ucModel[i].lblDeri.Text = ds.Tables[0].Rows[i]["deri_kodu"].ToString();
                    //ucModel[i].lblRenk.Text = ds.Tables[0].Rows[i]["renk_kodu"].ToString();
                    //ucModel[i].lblAstar.Text = ds.Tables[0].Rows[i]["astar_kodu"].ToString();
                    //ucModel[i].lblLazer.Text = ds.Tables[0].Rows[i]["lazer_kodu"].ToString();
                    //ucModel[i].lblKalite.Text = ds.Tables[0].Rows[i]["kalite_no"].ToString();
                    //ucModel[i].lblKalite.Text = ds.Tables[0].Rows[i]["kalite_no"].ToString();
                    //ucModel[i].Location = new Point(((440 + firstColumn) * column) + firstColumn, (216 * row) + 10);
                    
                    

                    ucModel[i].peResim.Name = ds.Tables[0].Rows[i]["model_id"].ToString();
                    //ucModel[i].lblKalite.Text = ds.Tables[0].Rows[i]["kalite_no"].ToString();
                    ucModel[i].lblKalite.Text = ds.Tables[0].Rows[i]["kalitebilgileri"].ToString();
                    ucModel[i].lblTaban.Text = ds.Tables[0].Rows[i]["tabanbilgileri"].ToString();
                    ucModel[i].lblDeri.Text = ds.Tables[0].Rows[i]["deribilgileri"].ToString();
                    ucModel[i].lblRenk.Text = ds.Tables[0].Rows[i]["renkbilgileri"].ToString();
                    ucModel[i].lblAstar.Text = ds.Tables[0].Rows[i]["astarbilgileri"].ToString();
                    ucModel[i].lblLazer.Text = ds.Tables[0].Rows[i]["lazerbilgileri"].ToString();
                    ucModel[i].lblMakineSarac.Text = ds.Tables[0].Rows[i]["makinesaracbilgileri"].ToString();
                    ucModel[i].lblElSarac.Text = ds.Tables[0].Rows[i]["elsaracbilgileri"].ToString();
                    ucModel[i].Location = new Point(((440 + firstColumn) * column) + firstColumn, (216 * row) + 10);




                    //ucModel[i].Size = new Size(175, 175);
                    //ucModel[i].Properties.SizeMode = PictureSizeMode.Stretch;
                    xtraScrollableControl2.Controls.Add(ucModel[i]);
                    ucModel[i].peResim.Click += new EventHandler(ModeliFiseAktar_Click);
                }
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void tabanLookUp_QueryPopUp(object sender, CancelEventArgs e)
        {
            if (kalite == null)
            {
                FrmMessage.showDialog("Önce kalite seçiniz...", MessageTypes.Information);
                return;
            }
            LoadTaban();
        }

        private void ModeliFiseAktar_Click(object sender, EventArgs e)
        {
            modelId = Convert.ToInt32(((PictureEdit)sender).Name);
            this.Close();
        }

        private void FrmModelImages_ResizeEnd(object sender, EventArgs e)
        {
            LoadModeller();
        }

        private void FrmModelImages_SizeChanged(object sender, EventArgs e)
        {
            LoadModeller();
        }

        private void btnUygula_Click(object sender, EventArgs e)
        {
            LoadModeller();
        }

        private void btnTemizle_Click(object sender, EventArgs e)
        {
            clearForm();
            LoadModeller();
        }
        #endregion

    }
}
